//
//  RJCheckSendVC.m
//  AirQuality
//
//  Created by Po on 15/5/31.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJCheckSendVC.h"
#import "RJCheckSendModel.h"

@interface RJCheckSendVC ()<UIAlertViewDelegate>

@property (retain, nonatomic) UIButton  * sendButton;
@property (strong, nonatomic) RJCheckSendModel     * sendModel;

@property (retain, nonatomic) NSDictionary * data;
@property (strong, nonatomic) NSMutableDictionary * checkDic;       //是否可以审核；
@property (assign, nonatomic) BOOL        isCity;

@end

@implementation RJCheckSendVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getSendButton];
    _checkDic = [NSMutableDictionary dictionary];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkAllData:) name:@"SendAllData" object:nil];
}

#pragma mark - event
- (void)pressSendButton:(UIButton *)sender
{
//    NSArray * checkArray = [_checkDic allValues];
//    NSInteger count = 0;
//    for (NSString * string  in checkArray) {
//        NSInteger index = [string integerValue];
//        if (index != 0) {
//            count ++;
//        }
//    }
//    for (NSString * string  in checkArray) {
//        NSInteger index = [string integerValue];
//        NSString * alertString = [NSString stringWithFormat:@"有[%ld]个站点有疑问数据，请先审核数据!", count];
//        if (index != 0) {
//            UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:alertString delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
//            alertView.delegate = self;
//                    [alertView show];
//                    return;
//        }
//    }
    NSString * cityName;
    NSRange range = [_data[@"CITYNAME"] rangeOfString:@"["];
    if (range.location != NSNotFound) {
        cityName = [_data[@"CITYNAME"] substringWithRange:NSMakeRange(0, range.location)];
    }
    
    NSString * showString = [NSString stringWithFormat:@"确认提交【%@】的审核数据吗？",cityName];
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:showString preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * cancelAction= [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:cancelAction];
    UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self sendCheckData];
    }];
    [alert addAction:okAction];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - function
- (void)sendCheckData
{
    NSMutableArray * stationsDic= [NSMutableArray array];
    NSMutableArray * stationsType = [NSMutableArray array];
    NSArray * stationtypesArray = _data[@"STATIONTYPES"];
    for (NSDictionary * stations in stationtypesArray) {
        for (NSDictionary * dic in stations[@"STATIONS"]) {
            [stationsDic addObject:dic[@"STATIONCODE"]];
            NSString * type = dic[@"STATIONTYPEID"];
            NSInteger count = [stationsType indexOfObject:type];
            if (count == NSNotFound) {
                [stationsType addObject:type];
            }
        }
    }
    NSUserDefaults *userDefailt = [NSUserDefaults standardUserDefaults];
    NSString *userName = [userDefailt objectForKey:@"userName"];
    NSDictionary * request = @{
                @"stationtypecode":[stationsType componentsJoinedByString:@","],
                @"status":@"2",     //上传
                @"stationcode":[stationsDic componentsJoinedByString:@","],
                @"citycode":_data[@"CITYCODE"],
                @"timepoint":[RJTools getTimeWithFormat:@"yyyy-MM-dd 00:00:00" time:YesterdayDate],
                @"userName":userName
                };

    [RJNetWork myNetRequestWithAddress:[NetURL defaultURL].sendCheck data:request completeBlock:^(id responseObject) {
        if (responseObject[@"result"]) {
            if (responseObject[@"result"]) {
                NSLog(@"------%@",responseObject);
                NSString *status = [NSString stringWithFormat:@"%@",responseObject[@"result"]];
                if ([status isEqualToString:@"-1"]) {
                    [RJTools showTitle:@"失败" msg:@"数据未接入！" cancelString:@"确定" vc:self];
                }else if ([status isEqualToString:@"0"]){
                    [RJTools showTitle:@"失败" msg:@"还未下发,不能上传！" cancelString:@"确定" vc:self];
                }else if ([status isEqualToString:@"1"]){
                    [RJTools showTitle:@"失败" msg:@"已上传,不能重复上传！" cancelString:@"确定" vc:self];
                }else if ([status isEqualToString:@"2"]){
                    [RJTools showTitle:@"成功" msg:@"上传数据成功！" cancelString:@"知道了" vc:self];
                }else if ([status isEqualToString:@"3"]){
                    [RJTools showTitle:@"失败" msg:@"上传数据失败，请重试！" cancelString:@"确定" vc:self];
                }
            }
        }
    }failBlack:^(id response) {
        [RJTools showTitle:@"失败" msg:@"上传数据出现错误！" cancelString:@"知道了" vc:self];
    }];
}

#pragma mark - getter
- (UIButton *)getSendButton
{
    if (!_sendButton) {
//        _sendButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
//        [_sendButton setImage:[UIImage imageNamed:@"update"] forState:UIControlStateNormal];
//        [_sendButton setImageEdgeInsets:UIEdgeInsetsMake(10, 5, 10, 5)];
//        [_sendButton addTarget:self action:@selector(pressSendButton:) forControlEvents:UIControlEventTouchUpInside];
//        [self.view addSubview:_sendButton];
    }
    return _sendButton;
}

#pragma mark - setter
- (void)setSendData:(NSDictionary *)data
{
    _data = [NSDictionary dictionaryWithDictionary:data];
}

- (void)setIsCity:(BOOL)isCity
{
    _isCity = isCity;
}

- (void)checkAllData:(NSNotification *)notification{
    _checkDic = notification.object;
}

@end
